package aries.utils;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

public class TextUtil extends Util {
    
    /**
     * Extension filter.
     */
    private static String extension = "[/\\.]";
    
    /**
     * Convert field name in DM (Database Model) into real title text.
     * From:
     * this_is_field_title
     * To:
     * This Is Field Title
     * 
     * @param title
     * @return String
     */
    public static String makeTitle(String title) {
        title = replacePattern(title, "_");
        String[] titles = title.split(" ");
        String theTitle = "";
        for (byte i=0; i<titles.length; i++) {
            theTitle = theTitle+" "+upperFirstCase(titles[i]);
        }
        return theTitle;
    }
    
    /**
     * Get the extensions from a filename.
     *
     * @param filename
     * @return String
     */
    public static String findExtension(String filename) {
        filename = filename.toLowerCase();
        String[] exts = filename.split(extension);
        int n = exts.length - 1;
        return exts[n];
    }
    
    /**
     * Get the name from a filename.
     * 
     * @param filename
     * @return String
     */
    public static String findFileName(String filename) {
        filename = filename.toLowerCase();
        String[] exts = filename.split(extension);
        int n = exts.length - 2;
        return exts[n];
    }

    /**
     * Generate random String.
     *
     * @return String
     */
    public static String genRandomString() {
        return genRandomString((byte) 50);
    }

    /**
     * Generate random string.
     *
     * @param length
     * @return String
     */
    public static String genRandomString(byte length) {
        String[] character = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e",
            "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
            "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};
        String string = "";
        for (byte i = 0; i < length; i++) {
            string = string + character[(int) (java.lang.Math.random() * character.length)];
        }
        return string;
    }
    
    public static String genRandomAlphabet(byte length) {
        String[] character = {"a", "b", "c", "d", "e",
            "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
            "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};
        String string = "";
        for (byte i = 0; i < length; i++) {
            string = string + character[(int) (java.lang.Math.random() * character.length)];
        }
        return string;
    }
    
    /**
     * Generate random number.
     * 
     * @param length
     * @return String
     */
    public static String getRandomNumber(byte length) {
        String[] character = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        String string = "";
        for (byte i = 0; i < length; i++) {
            string = string + character[(int) (java.lang.Math.random() * character.length)];
        }
        return string;
    }

    /**
     * Replace pattern with a space.
     * Example:
     * You have String ex = "Text, Text";
     * After you call this method it will be "Text Text" without a comma and
     * comma replaced with a space.
     *
     * @param text
     * @param patern
     * @return String
     */
    public static String replacePattern(String text, String patern) {
        return replacePattern(text, patern, " ");
    }

    /**
     * Replace pattern with a separator.
     * Example:
     * You have String ex = "Text, Text";
     * Then you set the separator to "|"
     * After you call this method it will be "Text|Text" without a comma and
     * comma replaced with a separator.
     *
     * @param text
     * @param patern
     * @param separator
     * @return String
     */
    public static String replacePattern(String text, String patern, String separator) {
        String result = null;
        String process[] = text.split(patern);
        for (byte i = 0; i < process.length; i++) {
            result = new StringBuilder().append(result).append(separator).append(process[i]).toString();
        }
        return result.substring(1, result.length());
    }

    /**
     * Join an array text and set separator between it.
     * Example:
     * You have String[] ex = {"Text", "Text"};
     * Then you set the separator to <code>|</code>
     * After you call this method it will be <code>Text|Text</code>.
     *
     * @param text
     * @param separator
     * @return String
     */
    public static String join(String text[], String separator) {
        return replacePattern(text, separator, "");
    }

    /**
    Join an array text and set separator between it.
     * Example:
     * You have String[] ex = {"Text", "Text"};
     * Then you set the separator to <code>|</code>
     * After you call this method it will be <code>Text|Text</code>.
     * And when you specify a flank with some String ex: *.
     * The result is <code>*Text|Text*</code>.
     *
     * @param text
     * @param separator
     * @param flank
     * @return String
     */
    public static String replacePattern(String text[], String separator, String flank) {
        String result = "";
        for (byte i = 0; i < text.length; i++) {
            if (!flank.equals("")) {
                flank(text[i], flank);
            }
            result = (new StringBuilder()).append(result).append(separator).append(text[i]).toString();
        }
        return result.substring(1, result.length());
    }

    /**
     * Flanked a text.
     *
     * @param text
     * @return String
     */
    public static String flank(String text, String flank) {
        return flank + text + flank;
    }

    /**
     * Make the firstcase of the text to capital.
     *
     * @param text
     * @return String
     */
    public static String upperFirstCase(String text) {
        return (new StringBuilder()).append(text.substring(0, 1).toUpperCase()).append(text.substring(1, text.length())).toString();
    }
    
}
